package com.panda.expand;

import com.panda.pojo.Context;
import com.panda.pojo.Element;
import com.panda.utils.ExpandInit;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

/*
    如果语法糖是用来解析元素的可以直接继承该类
 */
public abstract class ElementSugar extends Sugar {

    protected Set<String> noBodyTags = ExpandInit.getNoBodyTags();
    protected Set<String> targets = ExpandInit.getTargets();

    @Override
    public void doParse(String e, Context context) {

        Element currentElement = context.getCurrentElement();

        if (currentElement.getExpect() == Element.BTP || currentElement.getExpect() == Element.BTB) {
            context.setCurrentElement(currentElement.getParent());
            reParse(e, context);
            return;
        }


        Element element = makeElement(e, context);

        if (!noBodyTags.contains(element.getName())) {
            element.setSubBlocks(new ArrayList<>());
        }

        currentElement.addSubBlock(element);

        if (currentElement.isAssigning()) {

            currentElement.setAssigning(false);

            Map<String, String> attributes = element.getAttributes();

            attributes.put("eid", element.getId());

            currentElement.appendTempText(element.getId()).appendTempText("\n\n");

        }

        element.setExpect(Element.BTP);

        context.setCurrentElement(element);


    }

    protected abstract Element makeElement(String e, Context context);

}
